package game.creator;

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
import com.jme3.scene.control.CameraControl;
import com.jme3.scene.control.CameraControl.ControlDirection;
import control.character.MyCharacterControl;
import control.character.SimpleAIControl;
import game.DataStore;
import game.constant.Constants;
import game.constant.DefaultValues;
import game.gameobject.character.CharacterSettings;

public abstract class CharacterCreator {

    public static Node createEnemy(Vector3f location) {
	//load character settings
	CharacterSettings charSettings = new CharacterSettings();
	charSettings.setHeight(DefaultValues.PLAYER_HEIGHT);
	charSettings.setRadius(DefaultValues.PLAYER_RADIUS);
	charSettings.setMass(DefaultValues.PLAYER_MASS);
	charSettings.setStepHeight(DefaultValues.PLAYER_STEP_HEIGHT);
	charSettings.setWalkSpeed(DefaultValues.PLAYER_WALK_SPEED);
        charSettings.setRunSpeed(DefaultValues.PLAYER_RUN_SPEED);
	charSettings.setSprintSpeed(DefaultValues.PLAYER_SPRINT_SPEED);

	//create character node
        Node enemy = (Node)DataStore.assetManager.loadModel("Models/Oto/Oto.mesh.xml");
	enemy.setUserData(Constants.CHARACTER_SETTINGS_OBJ, charSettings);
	enemy.scale(0.25f);
	enemy.setLocalTranslation(location);
	DataStore.characterHubNode.attachChild(enemy);
	
	//add camera control
	Camera enemyCam = DataStore.cam.clone();
	enemy.addControl(new CameraControl(enemyCam, ControlDirection.CameraToSpatial));

	//add character control
        CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(charSettings.getRadius(), charSettings.getHeight(), 1);
	MyCharacterControl characterControl = new MyCharacterControl(capsuleShape, charSettings.getStepHeight());
	enemy.addControl(characterControl);
	DataStore.bulletAppState.getPhysicsSpace().add(enemy);

	//add ai control
	SimpleAIControl aiControl = new SimpleAIControl();
	enemy.addControl(aiControl);
	DataStore.aiAppState.getAISpace().add(enemy);

	//add ghost control to physics space
	DataStore.bulletAppState.getPhysicsSpace().add(characterControl.getGhostControl());

	//add animation control and set up channels
	AnimControl animationControl = enemy.getControl(AnimControl.class);
	animationControl.addListener(characterControl);
	AnimChannel walkChannel = animationControl.createChannel();
	walkChannel.setAnim(Constants.CHARACTER_ANIM_STAND);
	AnimChannel rightArmChannel = animationControl.createChannel();
	rightArmChannel.addBone(animationControl.getSkeleton().getBone("uparm.right"));
	rightArmChannel.addBone(animationControl.getSkeleton().getBone("arm.right"));
	rightArmChannel.addBone(animationControl.getSkeleton().getBone("hand.right"));
	rightArmChannel.setAnim(Constants.CHARACTER_ANIM_STAND);
	AnimChannel leftArmChannel = animationControl.createChannel();
	leftArmChannel.addBone(animationControl.getSkeleton().getBone("uparm.left"));
	leftArmChannel.addBone(animationControl.getSkeleton().getBone("arm.left"));
	leftArmChannel.addBone(animationControl.getSkeleton().getBone("hand.left"));
	leftArmChannel.setAnim(Constants.CHARACTER_ANIM_STAND);

	//load character current values
	enemy.setUserData(Constants.CHARACTER_CURRENT_SPEED, charSettings.getWalkSpeed());

	//just for testing
	aiControl.setRandomWalkDirection();

        return enemy;
    }
}
